package com.wandian.audio.tools;

import android.text.TextUtils;
import android.util.Log;

import com.mylib.libcore.AppContext;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.UIUtils;

import java.security.MessageDigest;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * 请求参数md5
 */
public class TokenSignUtils {

    public static String getSignCode(Map<String, Object> treeMap,String code) {
        String mSignCode;
        String mInitCode = "";
        if(UIUtils.isEmpty(code)){
            mInitCode = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("music_sign");
        }else {
            mInitCode = code;
        }
        if(UIUtils.isEmpty(mInitCode)){
            mInitCode = "";
        }
        StringBuilder mAllValue = new StringBuilder();

        Map<String, String> rootMap = new TreeMap<>();
        rootMap.put("app-id", "10002");
        rootMap.put("app-secret","15A2730157F583C5421050A322E651C0");
        rootMap.put("device-version", "5.1.7");
        rootMap.put("device-uuid", "");
        String token = "";

        if (!TextUtils.isEmpty(token)) {
            rootMap.put("access-token", token);
        }
        treeMap.putAll(rootMap);
        Log.d("","");
        Set set = treeMap.entrySet();
        for (Object o : set) {
            Map.Entry mapEntry = (Map.Entry) o;
            mAllValue.append(mapEntry.getValue());
        }
        mSignCode =  encodeToMD5( encodeToMD5(mAllValue.toString()) + mInitCode);
        Log.d("请求","请求"+"参数sign："+mSignCode+"公共参数："+ JsonUtil.toJson(rootMap));
        AppContext.getInstance().getAppPreferencesHelper().setKeyValue("music_sign_md5",mSignCode+"");
        return mSignCode;
    }

    /**
     * MD5
     *
     * @param string
     * @return
     * @throws Exception
     */
    public static String encodeToMD5(String string) {
        byte[] hash = new byte[0];
        try {
            hash = MessageDigest.getInstance("MD5").digest(
                    string.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }
        return hex.toString();
    }

    public static  Map<String, Object> getMusicRequestMap(Map<String, Object> treeMap) {
        String mSignCode = AppContext.getInstance().getAppPreferencesHelper().getKeyValue("music_sign_md5");
        StringBuilder mAllValue = new StringBuilder();
        Map<String, Object> rootMap = new TreeMap<>();
        rootMap.put("app-id", "10002");
        rootMap.put("app-secret","15A2730157F583C5421050A322E651C0");
        rootMap.put("device-version", "5.1.7");
        rootMap.put("device-uuid", "");
        rootMap.put("sign", mSignCode+"");
        treeMap.putAll(rootMap);
        Set set = treeMap.entrySet();
        for (Object o : set) {
            Map.Entry mapEntry = (Map.Entry) o;
            mAllValue.append(mapEntry.getValue());
        }

        Log.d("请求","请求"+"参数sign："+mSignCode+"公共参数："+ JsonUtil.toJson(rootMap));
        return rootMap;
    }

}
